/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cnguifang.feigewallet;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.hyphenate.easeui.EaseUI;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.util.Locale;

import static com.cnguifang.feigewallet.DemoHelper.TAG;

public class DemoApplication extends Application {

	public final static String DEFAULT_SAVE_IMAGE_PATH = Environment.getExternalStorageDirectory() + File.separator + "CircleDemo" + File.separator + "Images"
			+ File.separator;
	public static Context applicationContext;
	private static DemoApplication instance;
	// login user name
	public final String PREF_USERNAME = "username";

	/**
	 * nickname for current user, the nickname instead of ID be shown when user receive notification from APNs
	 */
	public static String currentUserNick = "";
	private int language_id=0;

	@Override
	public void onCreate() {
		MultiDex.install(this);
		super.onCreate();
		//打开Log开关 正式发布时请关闭
        applicationContext = this;
        instance = this;
		Fresco.initialize(this);
		//init demo helper
        DemoHelper.getInstance().init(applicationContext);

        // 请确保环信SDK相关方法运行在主进程，子进程不会初始化环信SDK（该逻辑在EaseUI.java中）
        if (EaseUI.getInstance().isMainProcess(this)) {
			// 初始化华为 HMS 推送服务, 需要在SDK初始化后执行
			HMSPushHelper.getInstance().initHMSAgent(instance);
		}
		setLanguage();

		applicationContext = getApplicationContext();
		initImageLoader();
	}

	private void setLanguage() {
		Resources resources=getResources();
		final SharedPreferences sharedPreferences=getSharedPreferences("language_choice", getApplicationContext()
				.MODE_PRIVATE);
		language_id = sharedPreferences.getInt("language_id", 0);
		Log.e(TAG,"language_id=="+language_id);
		// 获取应用内语言
		final Configuration configuration=resources.getConfiguration();
//        Locale locale=configuration.locale;
		DisplayMetrics displayMetrics=resources.getDisplayMetrics();
		switch (language_id){
			case 0:
				configuration.locale= Locale.getDefault();
				break;
			case 1:
				configuration.locale=Locale.ENGLISH;
				break;
			default:
				configuration.locale=Locale.SIMPLIFIED_CHINESE;
				break;
		}
		Log.e(TAG,"configuration=="+configuration.locale);
		getResources().updateConfiguration(configuration,displayMetrics);
	}

	/** 初始化imageLoader */
	private void initImageLoader() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(com.yiw.circledemo.R.color.bg_no_photo)
				.showImageOnFail(com.yiw.circledemo.R.color.bg_no_photo).showImageOnLoading(com.yiw.circledemo.R.color.bg_no_photo).cacheInMemory(true)
				.cacheOnDisk(true).build();

		File cacheDir = new File(DEFAULT_SAVE_IMAGE_PATH);
		ImageLoaderConfiguration imageconfig = new ImageLoaderConfiguration.Builder(this)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.diskCacheSize(50 * 1024 * 1024)
				.diskCacheFileCount(200)
				.diskCache(new UnlimitedDiskCache(cacheDir))
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.defaultDisplayImageOptions(options).build();

		ImageLoader.getInstance().init(imageconfig);
	}

	public static DemoApplication getInstance() {
		return instance;
	}

	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		MultiDex.install(this);
	}
}
